﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Utils;
using GenericLibrariesToolKit;
using System.Transactions;

namespace BLL.Users.Impl
{
    /// <summary>
    /// 用户管理业务接口实现
    /// </summary>
    public class UserManageBOImpl : UserManageBO
    {
        //初始化用户公共操作接口
        UserBO userBO = new UserBOImpl();
        //初始化专家操作接口
        ExpertBO expertBO = new ExpertBOImpl();
        //初始化专家对象
        Expert expert = null;
        //初始化企业操作接口
        EnterpriseBO enterpriseBO = new EnterpriseBOImpl();
        //初始化企业对象
        Enterprise enterprise = null;

        public string Register(User user, object obj)
        {
            //新增执行结果，true：执行成功，false：执行失败
            bool result = false;
            //密码加密
            user.UserPassword = EncryptUtil.Encode(user.UserPassword);
            //查看注册用户名是否存在
            User userObj = userBO.Find(user.UserId);
            //用户名存在
            if (userObj != null)
            {
                //用户有效
                if (userObj.Visible == 1)
                {
                    return "User name already exists";
                }
                //用户过期，重新启用
                else if (userObj.Visible == 0)
                {
                    //是否通过审核更新为未通过
                    user.IsAuditing = 0;
                    //是否可用更新为可用
                    user.Visible = 1;
                    using (TransactionScope tx = new TransactionScope())
                    {
                        result = userBO.Update(user);
                        //更新专家信息
                        if (user.UserType == "Expert")
                        {
                            expert = (Expert)obj;
                            expert.ExpertId = user.UserId;
                            result = expertBO.Update(expert);
                        }
                        //更新企业信息
                        else if (user.UserType == "Enterprise")
                        {
                            enterprise = (Enterprise)obj;
                            enterprise.EnterpriseId = user.UserId;
                            result = enterpriseBO.Update(enterprise);
                        }
                        tx.Complete();
                    } 
                }
            }
            //用户名不存在
            else
            {
                using (TransactionScope tx = new TransactionScope())
                {
                    result = userBO.Add(user);

                    //专家注册
                    if (user.UserType == "Expert")
                    {
                        expert = (Expert)obj;
                        expert.ExpertId = user.UserId;
                        result = expertBO.Add(expert);
                    }
                    //企业注册
                    else if (user.UserType == "Enterprise")
                    {
                        enterprise = (Enterprise)obj;
                        enterprise.EnterpriseId = user.UserId;
                        result = enterpriseBO.Add(enterprise);
                    }
                    tx.Complete();
                }              
            }
            if (result)
            {
                return "Register succeed";
            }
            else
            {
                return "Register fail";
            }
        }

        public bool EidtProfile(string userId, object obj)
        {
            //编辑执行结果，true：执行成功，false：执行失败
            bool result = false;
            //通过用户id获取用户对象
            User user = userBO.Find(userId);
            //用户不存在
            if (user == null)
            {
                return result;
            }
            //修改专家信息
            if (user.UserType == "Expert")
            {
                expert = (Expert)obj;
                Expert temp = expertBO.Find(userId);
                expert.ExpertClickAmount = temp.ExpertClickAmount;
                expert.ExpertEmail = temp.ExpertEmail;
                result = expertBO.Update(expert);
            }
            //修改企业信息
            else if (user.UserType == "Enterprise")
            {
                enterprise = (Enterprise)obj;
                Enterprise temp = enterpriseBO.Find(userId);
                enterprise.EnterpriseEmail = temp.EnterpriseEmail;
                result = enterpriseBO.Update(enterprise);
            }

            return result;
        }

        public object ShowDetail(string userId)
        {
            //获取用户对象
            User user = userBO.Find(userId);

            if (user == null) return null;
            if (user.UserType.Equals("Expert"))
            {
                return expertBO.Find(userId);
            }
            else if (user.UserType.Equals("Enterprise"))
            {
                return enterpriseBO.Find(userId);
            }

            return null;
        }


        public string ShowUserDetail(string userId)
        {
            string url = "";
            User user = userBO.Find(userId);
            //用户不存在
            if (user == null)
            {
                return url;
            }
            //查询专家信息
            if (user.UserType == "Expert")
            {
                url = "../Expert/ExpertDetails.aspx?expertId=" + userId;
                //objList = expertList;
            }
            //查询企业信息
            else if (user.UserType == "Enterprise")
            {
                url = "../Enterprise/EnterpriseDetails.aspx?enterpriseId=" + userId;
            }
            return url;
        }
    }
}
